"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var UITabItem_1 = require("../controls/UITabItem");
var CastHelper = require("../utils/CastHelper");
var TypeHelper = require("../utils/TypeHelper");
var ExceptionHelper = require("../utils/ExceptionHelper");
var ElementHelper = require("../utils/ElementHelper");
var UITabContent_1 = require("../controls/UITabContent");
var App = (function () {
    function App(tabx) {
        this.tabx = tabx;
        this.version = "1.0.0";
        this.prefix = "tabx-";
        this._item = null;
        this._lastItem = null;
        this._items = [];
    }
    Object.defineProperty(App.prototype, "item", {
        get: function () {
            return this._item;
        },
        set: function (value) {
            this.tabx.item = this._item = value;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(App.prototype, "lastItem", {
        get: function () {
            return this._lastItem;
        },
        set: function (value) {
            this.tabx.lastItem = this._lastItem = value;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(App.prototype, "items", {
        get: function () {
            return this._items;
        },
        enumerable: true,
        configurable: true
    });
    App.prototype.open = function (options) {
        var _a = CastHelper.tupleStringOrElementCast(options.container), itemContainer = _a[0], contentContainer = _a[1];
        var itemContainerElement = typeof itemContainer === "string" ? document.querySelector(itemContainer) : itemContainer;
        var contentContainerElement = typeof contentContainer === "string" ? document.querySelector(contentContainer) : contentContainer;
        if (!itemContainerElement || !contentContainerElement)
            return;
        ElementHelper.addStyles(itemContainerElement, {
            position: "relative"
        });
        ElementHelper.addStyles(contentContainerElement, {
            position: "relative"
        });
        var item = this.getItem(options.item.id);
        if (!item) {
            item = new UITabItem_1.default(this, options.item);
            var tabItemElemnt = item.present();
            itemContainerElement.appendChild(tabItemElemnt);
            var uiContent = new UITabContent_1.default(this, item.id, item.url);
            var tabContentElement = uiContent.present();
            contentContainerElement.appendChild(tabContentElement);
            item.setComponent("tab-content", uiContent);
            item.content = uiContent;
            this.items.push(item);
        }
        item.switch();
    };
    App.prototype.getItem = function (id) {
        if (!TypeHelper.isStringWithNotEmpty(id))
            ExceptionHelper.assertId();
        for (var _i = 0, _a = this.items; _i < _a.length; _i++) {
            var item = _a[_i];
            if (item.id === id) {
                if (item.element) {
                    return item;
                }
                else {
                    var index = this.items.indexOf(item);
                    this.items.splice(index, 1);
                    return null;
                }
            }
        }
        return null;
    };
    return App;
}());
exports.default = App;
